﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VIT.Pre
{
    internal class WSCollection : System.Collections.ICollection
    {
        private WSEnumerableDelegate _action;
        int _bufferSizeService;
        int _count;

        public WSCollection(WSEnumerableDelegate action, int bufferSizeService = 10)
        {
            _action = action;
            _bufferSizeService = bufferSizeService;
            _count = -1;
        }

        void System.Collections.ICollection.CopyTo(Array array, int index)
        {
            var reader = (this as System.Collections.ICollection).GetEnumerator();
            while (reader.MoveNext())
            {
                array.SetValue(reader.Current, index);
                index++;
            }
        }

        int System.Collections.ICollection.Count
        {
            get
            {
                if (_count == -1)
                {
                    _action(0, -1, ref _count);
                }
                return Convert.ToInt32(_count);
            }
        }

        bool System.Collections.ICollection.IsSynchronized
        {
            get { return false; }
        }

        object System.Collections.ICollection.SyncRoot
        {
            get { return this; }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new WSEnumerator(_action, _bufferSizeService, _count);
        }
    }

    static public class WSGlobal
    {
        static public System.Collections.ICollection WSCollection(WSEnumerableDelegate action, int bufferSizeService = 10)
        {
            return new WSCollection(action, bufferSizeService);
        }
    }
}